package comic;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.drawable.Drawable;

public class ASofterWorld extends Comic {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8375261593867187145L;

	@Override
	public String get_display_name() {
		return "A Softer World";
	}

	@Override
	public int get_first_comic_n() {
		return 1;
	}

	@Override
	public int getLatest() throws MalformedURLException, IOException {
		String content = get_website_content("http://www.asofterworld.com/");
		String result = content.replaceAll(
				".*<TITLE>a softer world: (\\d+)</TITLE>.*", "$1");
		return Integer.parseInt(result);
	}

	@Override
	public Strip getStrip(int id) throws MalformedURLException, IOException {
		int latest = getLatest();
		Strip s = new Strip();

		if (id < first_comic)
			id = first_comic;
		else if (id > latest)
			id = latest;
		s.id = id;

		String url = String.format(
				"http://www.asofterworld.com/index.php?id=%d", id);
		String content = get_website_content(url);

		String regex = ".*title=\"(.*?)\" onClick.*";
		s.title = content.replaceAll(regex, "$1");

		regex = ".*<img style=\"border:none;\" src=\"(.*?)\".*";
		String result = content.replaceAll(regex, "$1");
		URL image_url = new URL("http://www.asofterworld.com/" + result);

		s.image = Drawable.createFromStream(image_url.openStream(), "");

		s.text = "";

		return s;
	}

}
